package cn.lj.logistics.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.tags.EditorAwareTag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lj.logistics.pojo.MsgObject;
import cn.lj.logistics.pojo.BaseData;
import cn.lj.logistics.pojo.Customer;
import cn.lj.logistics.pojo.CustomerExample;
import cn.lj.logistics.pojo.Customer;
import cn.lj.logistics.pojo.CustomerExample;
import cn.lj.logistics.pojo.CustomerExample.Criteria;
import cn.lj.logistics.pojo.User;
import cn.lj.logistics.pojo.UserExample;
import cn.lj.logistics.service.BaseDataService;
import cn.lj.logistics.service.CustomerService;
import cn.lj.logistics.service.UserService;
import cn.lj.logistics.service.CustomerService;
import net.sf.jsqlparser.statement.delete.Delete;

@Controller
@RequestMapping("/customer")
public class CustomerController {
     
	@Autowired
	private CustomerService customerService;
	
	@Autowired 
	private UserService userService;
	
	@Autowired
	private BaseDataService baseDataService;
	//编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m,Long customerId) {
		
		UserExample example=new UserExample();
		cn.lj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo("业务员");
		// 查询所有角色名称为业务员的管理员
		
		List<User> users = userService.selectByExample(example);
		m.addAttribute("users", users);
		System.out.println("所有的业务员-------："+users);
		
		//查询所有的区间数据
		List<BaseData> intervals = baseDataService.selectBaseDatasByParentName("区间管理");
		m.addAttribute("intervals", intervals);
		
		//修改操作
		if (customerId!=null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改回显数据
			Customer customer = customerService.selectByPrimaryKey(customerId);
			m.addAttribute("customer",customer);
		}
	
		return "customerEdit";
		
	}
	

	
	//新增功能
		@RequestMapping("/insert")
		@ResponseBody
		public MsgObject insert(Customer customer) {
			
			
		
		int row = customerService.insert(customer);
			MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！！");
			if (row==1) {
				mo=MsgObject.createMO(1, "新增数据成功");
			}
			return mo;
			
		}
		
		//修改功能
				@RequestMapping("/update")
				@ResponseBody
				public MsgObject update(Customer customer) {
				
				
				int row = customerService.updateByPrimaryKeySelective(customer);
					MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！！");
					if (row==1) {
						mo=MsgObject.createMO(1, "修改数据成功");
					}
					return mo;
					
				}
		
	
	//删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long customerId) {
		
	

		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
	
		
		int row = customerService.deleteByPrimaryKey(customerId);
	
		if (row==1) {
			mo=MsgObject.createMO(1, "删除数据成功");
		}
		return mo;
		
	}
	

	
	@RequestMapping("/customerPage")
	@RequiresPermissions("customer:customerPage")
	public String customerPage() {
		
		return "customerPage";
		
	}
	

	@RequestMapping("/list")
	@RequiresPermissions("customer:list")
	@ResponseBody
	public PageInfo<Customer> list(@RequestParam(defaultValue = "1")Integer pageNum,
			               @RequestParam(defaultValue = "10")Integer pageSize ,String keyword){
		System.out.println("keyword:"+ keyword);
		//开始分页
		PageHelper.startPage(pageNum, pageSize);
		CustomerExample example=new CustomerExample();
		Criteria criteria1 = example.createCriteria();
		//获取当前身份。判断当前是否是业务员，如果是业务员，就只能查看自己的客户
		Subject subject = SecurityUtils.getSubject();
		User user=(User) subject.getPrincipal();
		System.out.println("user："+user);
		
		if (user.getRolename().equals("业务员")) {
			// 查询当前业务员的客户
			criteria1.andRealnameEqualTo(user.getRealname());
		}
		//关键字不为空才做条件查询
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
		
		criteria1.andCustomerNameLike("%"+keyword+"%"); 

	
		}
		List<Customer> list = customerService.selectByExample(example);
		
		return  new PageInfo<Customer>(list);
		
	}
}
